﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules
{
    class LTD: BaseRule
    {
         
        double MIN_HOME_ODDS = 1;
        double MAX_HOME_ODDS = 2;
        double MIN_AWAY_ODDS = 2;
        double MAX_AWAY_ODDS = 5;
        double MIN_DRAW_ODDS = 2;
        double MAX_DRAW_ODDS = 5;
        double MIN_MATCHODDS_MATCHED = 2000;
        double MAX_OVER25_ODDS = 1.8;

        public LTD(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices): base()
        {
        

            MarketsUsedInside = new string[] { "match odds", "over/under 2.5 goals" };

            try
            {
                MIN_HOME_ODDS = Convert.ToDouble(config["Rule.LTD.MIN_HOME_ODDS"]);
                MAX_HOME_ODDS = Convert.ToDouble(config["Rule.LTD.MAX_HOME_ODDS"]);
                MIN_AWAY_ODDS = Convert.ToDouble(config["Rule.LTD.MIN_AWAY_ODDS"]);
                MAX_AWAY_ODDS = Convert.ToDouble(config["Rule.LTD.MAX_AWAY_ODDS"]);
                MIN_DRAW_ODDS = Convert.ToDouble(config["Rule.LTD.MIN_DRAW_ODDS"]);
                MAX_DRAW_ODDS = Convert.ToDouble(config["Rule.LTD.MAX_DRAW_ODDS"]);
                MIN_MATCHODDS_MATCHED = Convert.ToDouble(config["Rule.LTD.MIN_MATCHODDS_MATCHED"]);
                MAX_OVER25_ODDS = Convert.ToDouble(config["Rule.LTD.MAX_OVER25_ODDS"]);

            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString(), true, true);
               
            }

            string criteria = @"" + MIN_HOME_ODDS + "/" +
            MAX_HOME_ODDS + "/" +
            MIN_AWAY_ODDS + "/" +
            MAX_AWAY_ODDS + "/" +
            MIN_DRAW_ODDS + "/" +
            MAX_DRAW_ODDS + "/" +
            MIN_MATCHODDS_MATCHED + "/" +
            MAX_OVER25_ODDS;

            Logger.Log("============================================================================", true, true);
            Logger.Log("Matches qualified for Lay the Draw (" + criteria + ")", true, true);
            Logger.Log("----------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; } 

            string[] games = FindSoccerGames.GetMatches();

            int selectionCount = 0;
            foreach (var game in games)
            {
                try
                {
                    MyMarket market_ou = FindSoccerGames.GetMarket(game, "Over/Under 2.5 goals");
                    MarketPricesCompressed mpc_ou = marketprices[market_ou.marketId];

                    //MyMarket market_cs = FindSoccerGames.GetMarket(game, "correct score");
                    //MarketPricesCompressed mpc_cs = marketprices[market_cs.marketId];

                    MyMarket market_mo = FindSoccerGames.GetMarket(game, "match odds");
                    MarketPricesCompressed mpc_mo = marketprices[market_mo.marketId];


                    double homeodds = mpc_mo.GetBestBackOdds(0);
                    double awayodds = mpc_mo.GetBestBackOdds(1);
                    double drawodds = mpc_mo.GetBestLayOdds(2);
                    double matched = mpc_mo.TotalMatched;

                    double o25odds = mpc_ou.GetBestBackOdds(1);


                    if ((homeodds >= MIN_HOME_ODDS && homeodds <= MAX_HOME_ODDS) &&
                       (awayodds >= MIN_AWAY_ODDS && awayodds <= MAX_AWAY_ODDS) &&
                       (drawodds >= MIN_DRAW_ODDS && drawodds <= MAX_DRAW_ODDS) &&
                        matched >= MIN_MATCHODDS_MATCHED &&
                        o25odds <= MAX_OVER25_ODDS)
                    {
                        Logger.Log(market_mo.marketTime + ", " + BF.convertPathToGame(game) + ", home: " + homeodds + ", away: " + awayodds + ", lay draw: " + drawodds + ", " + config["CURRENCY_SIGN"] + matched + ", over 2.5: " + o25odds, true, true);
                        selectionCount++;
                    }
                }
                catch (Exception  ex)
                {

                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);

                }

            }
            if (selectionCount == 0)
            {
                Logger.Log("No matches", true, true);
            }
        }
    
    }
}
